//
//  OrderDetail.m
//  ChampoinPeso
//
//  Created by mac on 2025/2/19.
//

#import "OrderDetail.h"
#import "OrderCell.h"
#import "OrderNoSourceCell.h"
#import "AppDelegate.h"
#import "TabBarViewController.h"
#import "DataFrameWeb.h"
#import "MJRefresh/MJRefresh.h"
#import "Setting.h"
#import "Login.h"
@interface OrderDetail ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic,strong)NSString *suddenchange;
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic,strong)NSMutableArray *source;
@property (nonatomic,strong)NSMutableDictionary *now;
@end

@implementation OrderDetail
-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    NSInteger i = self.index - 999;
    if (i == 0) {
        self.suddenchange = @"1";
    }else if (i == 1){
        self.suddenchange = @"3";
    }else if (i == 2){
        self.suddenchange = @"5";
    }else if (i == 3){
        self.suddenchange = @"2";;
    }else if (i == 4){
        self.suddenchange = @"4";
    }else if (i == 5){
        self.suddenchange = @"6";
    }
    [self detailSource];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = UIColor.whiteColor;
    NSInteger i = self.index - 999;
    if (i == 0) {
        self.navtitleText = @"Under review";
    }else if (i == 1){
        self.navtitleText = @"Overdue";
    }else if (i == 2){
        self.navtitleText = @"Loan released";
    }else if (i == 3){
        self.navtitleText = @"Loan in progress";
    }else if (i == 4){
        self.navtitleText = @"Review failed";
    }else if (i == 5){
        self.navtitleText = @"Cleared";
    }
    [self navUI];
    [self.view addSubview:self.tableView];
    [_tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.navView.mas_bottom).offset(0)
        ;
        make.left.right.bottom.mas_offset(0)
        ;    }];

}
///cham/sending
-(void)detailSource{
    NSDictionary *params = @{@"suddenchange":self.suddenchange};
    [self.tableView.mj_header endRefreshing];
    [DataFrameMove frameMoverForPost:@"/cham/sending" MoverPost:YES frameParm:params finish:^(id  _Nonnull info) {
        self.source = [NSMutableArray arrayWithArray:info[@"fashion"][@"chieftain"]];
        [self.tableView reloadData];
    }];
}
-(UITableView *)tableView
{
    if (!_tableView) {
        _tableView = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStylePlain];
        _tableView.backgroundColor = UIColor.clearColor;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.showsHorizontalScrollIndicator = NO;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.mj_header = [MJRefreshGifHeader headerWithRefreshingBlock:^{
            [self detailSource];
        }];
    }
    return _tableView;
}
-(NSMutableArray *)source
{
    if (!_source) {
        _source = [NSMutableArray array];
    }
    return _source;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (self.source.count == 0) {
        return 1;
    }
    return _source.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (self.source.count == 0) {
        OrderNoSourceCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([OrderNoSourceCell class])];
        if (cell == nil) {
            cell = [[OrderNoSourceCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:NSStringFromClass([OrderNoSourceCell class])];
        }
        cell.backgroundColor = UIColor.clearColor;
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        [cell.apply addTarget:self action:@selector(goHome) forControlEvents:UIControlEventTouchUpInside];
        return cell;
    }else{
        OrderCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([OrderCell class])];
        if (cell == nil) {
            cell = [[OrderCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:NSStringFromClass([OrderCell class])];
        }
        cell.source = self.source[indexPath.row];
        self.now = [NSMutableDictionary dictionaryWithDictionary:self.source[indexPath.row]];
        cell.backgroundColor = UIColor.clearColor;
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
//        [cell.check addTarget:self action:@selector(check) forControlEvents:UIControlEventTouchUpInside];
        [cell.agree addTarget:self action:@selector(agree) forControlEvents:UIControlEventTouchUpInside];
        return cell;
    }
    return nil;
}

-(void)goHome
{
    [self.navigationController popToRootViewControllerAnimated:NO];
    AppDelegate *delegate=(AppDelegate *)[[UIApplication sharedApplication] delegate];
    TabBarViewController* vc =  delegate.root;
    [vc selectIndex:0];
}

-(void)check:(NSDictionary *)dict
{
    NSString *str = CPS(dict[@"takes"]) ;
    if ([str containsString:@"http"]) {
        DataFrameWeb *web = [DataFrameWeb new];
        web.frameTo = str;
        web.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:web animated:YES];
    }else{
        if ([str containsString:@"jxe://ilgb.bbh.bjwv/wasouta"]) {
            Setting *vc = [Setting new];
            vc.hidesBottomBarWhenPushed = YES;
            [self.navigationController pushViewController:vc animated:YES];
        }else if ([str containsString:@"jxe://ilgb.bbh.bjwv/wasoutb"]) {
            [self.navigationController popToRootViewControllerAnimated:NO];
            AppDelegate *delegate=(AppDelegate *)[[UIApplication sharedApplication] delegate];
            TabBarViewController* vc =  delegate.root;
            [vc selectIndex:0];
        }else if ([str containsString:@"jxe://ilgb.bbh.bjwv/wasoutc"]) {
            Login *login = [Login new];
            [self presentViewController:login animated:YES completion:nil];
        }else if ([str containsString:@"jxe://ilgb.bbh.bjwv/wasoutd"]) {
            AppDelegate *delegate=(AppDelegate *)[[UIApplication sharedApplication] delegate];
            TabBarViewController* vc =  delegate.root;
            [vc selectIndex:1];
            NSArray *item = [str componentsSeparatedByString:@"="];
            NSString *tab = item[1];
            NSDictionary *userInfo = @{@"tab": tab};
            [[NSNotificationCenter defaultCenter] postNotificationName:@"gotoOrder" object:self userInfo:userInfo];
        }else if ([str containsString:@"jxe://ilgb.bbh.bjwv/wasoute"]) {
            [self.navigationController popToRootViewControllerAnimated:NO];
            AppDelegate *delegate=(AppDelegate *)[[UIApplication sharedApplication] delegate];
            TabBarViewController* vc =  delegate.root;
            [vc selectIndex:0];
            NSArray *item = [str componentsSeparatedByString:@"="];
            NSString *pid = item[1];
            NSDictionary *userInfo = @{@"pid": pid};
            [[NSNotificationCenter defaultCenter] postNotificationName:@"gotoDetail" object:self userInfo:userInfo];
        }
    }
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSDictionary *dict = self.source[indexPath.row];
    [self check:dict];
}
-(void)agree
{
    DataFrameWeb *web = [DataFrameWeb new];
    web.name = CPS(self.now[@"mill"]) ;
    web.frameTo = CPS(self.now[@"bien"]) ;
    [self.navigationController pushViewController:web animated:YES];
}

-(NSMutableDictionary *)now
{
    if (!_now) {
        _now  = [NSMutableDictionary dictionary]
    ;
    }
    return _now;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (self.source.count == 0) {
        return SCREEN_HEIGHT - 20- 64-MarginTop -STATUS_BAR_HEIGHT;
    }
    return UITableViewAutomaticDimension;
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
